package com.aj.activity;

import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.aj.activity.view.GestureContentView;
import com.aj.activity.view.GestureDrawl.GestureCallBack;
import com.aj.sharepreferences.LoveHomeSharedPreferences;

public class GestureActivity extends BaseActivity {

	private static final String TAG = GestureActivity.class.getName();

	private FrameLayout body_layout;
	private GestureContentView content;
	private int index = 0;
	private String passwordString = "";
	private TextView alertmessage_text;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_gesture);
		initView();
	}

	private void initView() {
		setActionBarTitle(R.string.safe_update_gesture_password_text);
		body_layout = (FrameLayout) findViewById(R.id.body_layout);
		alertmessage_text = (TextView) findViewById(R.id.alertmessage_text);
		content = new GestureContentView(this, "", new GestureCallBack() {
			@Override
			public void checkedSuccess() {
				verificationGesture();
			}

			@Override
			public void checkedFail() {
				verificationGesture();
			}
		});
		content.setParentView(body_layout);
	}

	private void verificationGesture() {
		if (index == 0) {
			index++;
			passwordString = content.getDrawl().getPassWordSb().toString();
			alertmessage_text.setText(getString(R.string.unlock_pic_two_text));
		} else if (index == 1) {
			if (passwordString.trim().equals(
					content.getDrawl().getPassWordSb().toString())) {
				Toast.makeText(
						GestureActivity.this,
						getString(R.string.safe_update_gesture_password_success_text),
						Toast.LENGTH_LONG).show();
				LoveHomeSharedPreferences.insertGesturePassowrd(passwordString);
				finish();
			} else {
				Toast.makeText(
						GestureActivity.this,
						getString(R.string.safe_update_gesture_password_error_text),
						Toast.LENGTH_LONG).show();
				index = 0;
				passwordString = "";
				alertmessage_text.setText(getString(R.string.unlock_pic_text));
			}
		}
	}
}
